type TimeFormat =
  | 'YYYY-MM-DD'
  | 'YYYY-MM-DD HH:mm:ss'
  | 'HH:mm:ss'
  | 'YYYY'
  | 'YYYY-MM'
  | 'DD'
  | 'MM'
  | 'YYYY年MM月'

// 格式化时间
export const formatTime = (time: Date, format: TimeFormat) => {
  const year = time.getFullYear()
  const month = time.getMonth() + 1
  const day = time.getDate()
  const hour = time.getHours()
  const minute = time.getMinutes()
  const second = time.getSeconds()
  const pad = (n: number) => (n < 10 ? `0${n}` : n)
  switch (format) {
    case 'YYYY年MM月':
      return `${year}年${pad(month)}月`
    case 'YYYY-MM-DD':
      return `${year}-${pad(month)}-${pad(day)}`
    case 'YYYY-MM-DD HH:mm:ss':
      return `${year}-${pad(month)}-${pad(day)} ${pad(hour)}:${pad(minute)}:${pad(second)}`
    case 'HH:mm:ss':
      return `${pad(hour)}:${pad(minute)}:${pad(second)}`
    case 'YYYY':
      return `${year}`
    case 'YYYY-MM':
      return `${year}-${pad(month)}`
    case 'DD':
      return `${day}`
    case 'MM':
      return `${month}`
  }
}
